<?php
include('config.php');
$_SESSION['erlogin'] = "";
$request = str_replace(get_option('install_path'), '', urldecode(add_trailing_slash(get_global($_SERVER, 'REQUEST_URI'))));
set_page_info('redirect', add_trailing_slash(remove_leading_slash(preg_replace('#/page/[0-9]+/#', '', $request))));

$website = get_option('website');
$install_path = get_option('install_path');
$website_path = $website.$install_path;


$posts->total = 0;
$s = '';
if (get_option('pretty_permalinks')) {
	if (preg_match('#page/([0-9]+)/$#', $request, $matches)) {
		$_GET['page'] = (int)$matches['1'];
		$request = str_replace($matches['0'], '', $request);
	}
	
	if (isset($_GET['search'])) {
		$search = get_global($_GET, 'search');
		if (empty($search)) header('Location: '.get_option('site_path'));
		else header('Location: search/'.urlencode($search).'/');
	}
	if (empty($request) || $request == '/') {
		$posts = get_posts(array(
			'perpage' => get_option('index_pagination')
		));
		
		include_theme_file('main.php', array('posts'));
		
		exit;
	} elseif (preg_match('#^/entry/([a-z0-9-]+)/$#', $request, $matches)) {
		$posts = get_posts(array(
			'where' => '`post_title_slug` = "'.escape($matches['1']).'"',
			'limit' => 1
		));
		
		if ($posts->total > 0) {
			$comments = get_comments(array(
				'where' => '`comment_post_id` = "'.$posts->items['0']->get_id().'"'
			));
		}
		
		session_start();
		$_SESSION['commentSpam'] = get_rand();
		$_SESSION['commentAlt'] = '';
		set_page_info('id', 'single');
		include_theme_file('single.php', array('posts', 'comments'));
		
		exit;
	} elseif (preg_match('#^/id/(\d+)/$#', $request, $matches)) {
		$posts = get_posts(array(
			'where' => '`post_id` = "'.escape($matches['1']).'"',
			'limit' => 1
		));
		
		if ($posts->total > 0) {
			$comments = get_comments(array(
				'where' => '`comment_post_id` = "'.$posts->items['0']->get_id().'"'
			));
		}
		
		session_start();
		$_SESSION['commentSpam'] = get_rand();
		$_SESSION['commentAlt'] = '';
		set_page_info('id', 'single');
		include_theme_file('single.php', array('posts', 'comments'));
		
		exit;
	} elseif (preg_match('#^/p/([a-z0-9-]+)/$#', $request, $matches)) {
		$pages = get_pages(array(
			'where' => '`page_title_slug` = "'.escape($matches['1']).'"',
			'limit' => 1
		));
		$thePages = array('page');
		
		if ($pages->total > 0) {
			$thePages[] = $pages->items['0']->get_slug();
			$thePages[] = $pages->items['0']->get_id();
		}
		
		set_page_info('id', $thePages);
		include_theme_file('page.php', array('pages'));
		
		exit;
	} elseif (preg_match('#^/category/([a-z0-9-]+)#', $request, $matches)) {
		$category = get_category('category_slug', escape($matches['1']));
		
		if (!empty($category)) {
			$posts = get_posts(array(
				'where' => 'FIND_IN_SET("'.$category->get_id().'", `post_category`)',
				'perpage' => get_option('archive_pagination')
			));
		}
		
		set_page_info('id', 'category');
		include_theme_file(array('category.php', 'index.php'), array('posts', 'category'));
		
		exit;
	} elseif (preg_match('#^/user/([a-zA-Z0-9_-]{4,18})#', $request, $matches)) {
		$user = get_single_item(array(
			'table' => USERS,
			'class' => 'user',
			'where' => '`user_username` = "'.escape($matches['1']).'"'
		));
		
		if (!empty($user)) {
			$posts = get_posts(array(
				'where' => '`post_author` = "'.$user->get_id().'"',
				'perpage' => get_option('archive_pagination')
			));
		}
		
		set_page_info('id', 'user');
		include_theme_file(array('category.php', 'index.php'), array('posts', 'user'));
		
		exit;
	} elseif (preg_match('#^/search/(.*)/#', $request, $matches)) {
		$s = clean(urldecode($matches['1']));
		$posts = get_posts(array(
			'where' => '`post_title` LIKE "%'.escape($s).'%" OR `post_content` LIKE "%'.$s.'%"',
			'perpage' => get_option('archive_pagination')
		));
		
		set_page_info('id', 'user');
		include_theme_file(array('search.php', 'index.php'), array('posts', 's'));
		
		exit;
	} elseif (preg_match('#^/comment/#', $request)) {
		include_file('comment.php', array('tablePrefix'));
	} elseif (preg_match('#^/Tips/#', $request)) {
	include_file('tips.php', array('tablePrefix'));
	} elseif (preg_match('#^/(feed|rss)/$#', $request)) {
		include_file('feed.php');
	} else {
		include_theme_file(array('404.php', 'index.php'));
	}
} else {
	if (empty($request) || $request == '/') {
		$posts = get_posts(array(
			'perpage' => get_option('index_pagination')
		));
		//Show mainpage 
		include_theme_file('main.php', array('posts'));
		
		exit;
	} elseif (!empty($_GET['entry']) && preg_match('#^([a-z0-9-]+)$#', $_GET['entry'], $matches)) {
		$posts = get_posts(array(
			'where' => '`post_title_slug` = "'.escape($matches['1']).'"',
			'limit' => 1
		));
		
		if ($posts->total > 0) {
			$comments = get_comments(array(
				'where' => '`comment_post_id` = "'.$posts->items['0']->get_id().'"'
			));
		}
		
		session_start();
		$_SESSION['commentSpam'] = get_rand();
		$_SESSION['commentAlt'] = '';
		set_page_info('id', 'single');
		include_theme_file('single.php', array('posts', 'comments'));
		
		exit;
	} elseif (!empty($_GET['id']) && preg_match('#^(\d+)$#', $_GET['id'], $matches)) {
		$posts = get_posts(array(
			'where' => '`post_id` = "'.escape($matches['1']).'"',
			'limit' => 1
		));
		
		if ($posts->total > 0) {
			$comments = get_comments(array(
				'where' => '`comment_post_id` = "'.$posts->items['0']->get_id().'"'
			));
		}
		
		session_start();
		$_SESSION['commentSpam'] = get_rand();
		$_SESSION['commentAlt'] = '';
		set_page_info('id', 'single');
		include_theme_file('single.php', array('posts', 'comments'));
		
		exit;
	} elseif (!empty($_GET['p']) && preg_match('#^([a-z0-9-]+)$#', $_GET['p'], $matches)) {
		$pages = get_pages(array(
			'where' => '`page_title_slug` = "'.escape($matches['1']).'"',
			'limit' => 1
		));
		$thePages = array('page');
		
		if ($pages->total > 0) {
			$thePages[] = $pages->items['0']->get_slug();
			$thePages[] = $pages->items['0']->get_id();
		}
		
		set_page_info('id', $thePages);
		include_theme_file('page.php', array('pages'));
		
		exit;
	} elseif (!empty($_GET['category']) && preg_match('#^([a-z0-9-]+)#', $_GET['category'], $matches)) {
		$category = get_category('category_slug', escape($matches['1']));
		
		if (!empty($category)) {
			$posts = get_posts(array(
				'where' => 'FIND_IN_SET("'.$category->get_id().'", `post_category`)',
				'perpage' => get_option('archive_pagination')
			));
		}
		
		set_page_info('id', 'category');
		include_theme_file(array('category.php', 'index.php'), array('posts', 'category'));
		
		exit;
	} elseif (!empty($_GET['user']) && preg_match('#^([a-zA-Z0-9_-]{4,18})#', $_GET['user'], $matches)) {
		$user = get_single_item(array(
			'table' => USERS,
			'class' => 'user',
			'where' => '`user_username` = "'.escape($matches['1']).'"'
		));
		
		if (!empty($user)) {
			$posts = get_posts(array(
				'where' => '`post_author` = "'.$user->get_id().'"',
				'perpage' => get_option('archive_pagination')
			));
		}
		
		set_page_info('id', 'user');
		include_theme_file(array('category.php', 'index.php'), 'posts');
		
		exit;
	} elseif (!empty($_GET['search'])) {
		$s = clean(urldecode(get_global($_GET, 'search')));
		$posts = get_posts(array(
			'where' => '`post_title` LIKE "%'.escape($s).'%" OR `post_content` LIKE "%'.$s.'%"',
			'perpage' => get_option('archive_pagination')
		));
		
		set_page_info('id', 'user');
		include_theme_file(array('search.php', 'index.php'), array('posts', 's'));
		
		exit;
	} elseif (ACTION == 'comment') {
		include_file('comment.php', array('tablePrefix'));
	} else {
		include_theme_file(array('404.php', 'index.php'));
	}
}
?>